package com.cloudsigns.factorymanager2.engineering.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IEngineeringInstallOrderDetailForTeamView;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.fragment.CompletionEvaluationForEngineeringInstallFragment;
import com.cloudsigns.factorymanager2.engineering.fragment.DailyFeedBackForEngineeringInstallFragment;
import com.cloudsigns.factorymanager2.engineering.fragment.WorkOrderInfomationForEngineeringInstallFragment;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringInstallOrderDetailForTeamPresenter;
import com.cloudsigns.factorymanager2.event.EngineeringOrderDetailForTeamEvent;
import com.cloudsigns.factorymanager2.project.adapter.ProjectMainViewPageAdapter;
import com.cloudsigns.factorymanager2.project.customview.CustomViewPager;
import com.cloudsigns.factorymanager2.project.customview.PageIndicatorView;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * Created by tony on 2018/4/6.
 */

public class TeamEngineeringInstallOrderFeedBackActivity extends FragmentActivity implements View.OnClickListener, IEngineeringInstallOrderDetailForTeamView {
    private final String TAG = TeamEngineeringInstallOrderFeedBackActivity.class.getSimpleName();
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_work_order_information, tv_daily_feed_back, tv_completion_order;
    private CustomViewPager viewpager;
    public static final int WORK_ORDER_INFORMATION = 0;
    public static final int DAILY_FEED_BACK = 1;
    public static final int COMPLETION_ORDER = 2;
    private ArrayList<Fragment> listFragments;
    private ProjectMainViewPageAdapter adapter;
    private WorkOrderInfomationForEngineeringInstallFragment workOrderInfomationForEngineeringInstallFragment;
    private DailyFeedBackForEngineeringInstallFragment dailyFeedBackForEngineeringInstallFragment;
    private CompletionEvaluationForEngineeringInstallFragment completionOrderForEngineeringInstallFragment;
    private View LL_work_order_information;
    private View ll_daily_feed_back;
    private View ll_completion_order;
    public int currentIndex = WORK_ORDER_INFORMATION;
    private EngineeringInstallOrderDetailForTeamPresenter engineeringInstallOrderDetailForTeamPresenter;
    private OrderParentBean orderParentBean;
    private EngineeringInstallationOrderBean engineeringInstallationOrderBean;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_engineering_install_order_for_super);

        engineeringInstallOrderDetailForTeamPresenter = new EngineeringInstallOrderDetailForTeamPresenter(this);
        init();


        Intent mIntent = getIntent();
        if (null != mIntent) {
            orderParentBean = (OrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_1);
        }

        EventBus.getDefault().register(this);// 注册

        getEngineeringOrderDetail();


        Intent intent = getIntent();
        if (null != intent) {
            orderParentBean = (OrderParentBean) intent.getSerializableExtra(ConstantUtil.KEY_1);
        }
        if (null != orderParentBean) {
            //  工程安装
            if (orderParentBean.getCategory() == CommonEnum.EngineeringORderCategory.INSTALL) {
                tv_title.setText(getResources().getText(R.string.engineering_installation));
            }
            // 批量安装
            else if (orderParentBean.getCategory() == CommonEnum.EngineeringORderCategory.BATCH_INSTALL) {
                tv_title.setText(getResources().getText(R.string.batch_installation));
            }
            // 广告上刊
            else if (orderParentBean.getCategory() == CommonEnum.EngineeringORderCategory.PUBLICATION) {
                tv_title.setText(getResources().getString(R.string.publication));
            }
        }


    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_work_order_information = findViewById(R.id.tv_work_order_information);
        tv_daily_feed_back = findViewById(R.id.tv_daily_feed_back);
        tv_completion_order = findViewById(R.id.tv_completion_order);

        viewpager = (CustomViewPager) findViewById(R.id.viewpager);
        LL_work_order_information = findViewById(R.id.LL_work_order_information);
        ll_daily_feed_back = findViewById(R.id.ll_daily_feed_back);
        ll_completion_order = findViewById(R.id.ll_completion_order);

        LL_work_order_information.setOnClickListener(this);
        ll_daily_feed_back.setOnClickListener(this);
        ll_completion_order.setOnClickListener(this);
        rl_back.setOnClickListener(this);


        workOrderInfomationForEngineeringInstallFragment = new WorkOrderInfomationForEngineeringInstallFragment();
        dailyFeedBackForEngineeringInstallFragment = new DailyFeedBackForEngineeringInstallFragment();
        completionOrderForEngineeringInstallFragment = new CompletionEvaluationForEngineeringInstallFragment();

        listFragments = new ArrayList<Fragment>();
        listFragments.add(workOrderInfomationForEngineeringInstallFragment);
        listFragments.add(dailyFeedBackForEngineeringInstallFragment);
        listFragments.add(completionOrderForEngineeringInstallFragment);


        adapter = new ProjectMainViewPageAdapter(getSupportFragmentManager(), listFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(2);

        PageIndicatorView indicator = (PageIndicatorView) findViewById(R.id.indicator);
        indicator.setViewPager(viewpager);
        indicator.setFades(false);
        indicator.setSelectedColor(getResources().getColor(R.color.rgb_2E75B6));
        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                showSelectedOption(position);

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    private void getEngineeringOrderDetail() {
        if (null != engineeringInstallOrderDetailForTeamPresenter)
            engineeringInstallOrderDetailForTeamPresenter.getEngineeringInstallOrderDetailForTeam();
    }


    /**
     * 切换到哪个tab页
     *
     * @param pos
     */
    private void showSelectedOption(int pos) {
        // TODO Auto-generated method stub
        switch (pos) {
            case WORK_ORDER_INFORMATION:
                LL_work_order_information.setSelected(true);
                ll_daily_feed_back.setSelected(false);
                ll_completion_order.setSelected(false);

                tv_work_order_information.setTextColor(getResources().getColor(R.color.rgb_000000));
                tv_daily_feed_back.setTextColor(getResources().getColor(R.color.rgb_A6A6A6));
                tv_completion_order.setTextColor(getResources().getColor(R.color.rgb_A6A6A6));
                break;
            case DAILY_FEED_BACK:
                LL_work_order_information.setSelected(false);
                ll_daily_feed_back.setSelected(true);
                ll_completion_order.setSelected(false);

                tv_work_order_information.setTextColor(getResources().getColor(R.color.rgb_A6A6A6));
                tv_daily_feed_back.setTextColor(getResources().getColor(R.color.rgb_000000));
                tv_completion_order.setTextColor(getResources().getColor(R.color.rgb_A6A6A6));

                break;
            case COMPLETION_ORDER:
                LL_work_order_information.setSelected(false);
                ll_daily_feed_back.setSelected(false);
                ll_completion_order.setSelected(true);

                tv_work_order_information.setTextColor(getResources().getColor(R.color.rgb_A6A6A6));
                tv_daily_feed_back.setTextColor(getResources().getColor(R.color.rgb_A6A6A6));
                tv_completion_order.setTextColor(getResources().getColor(R.color.rgb_000000));

                break;
            default:
                break;
        }

    }


    public void onEventMainThread(EngineeringOrderDetailForTeamEvent event) {
        getEngineeringOrderDetail();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.LL_work_order_information:
                if (currentIndex != WORK_ORDER_INFORMATION) {

                    currentIndex = WORK_ORDER_INFORMATION;
                    viewpager.setCurrentItem(WORK_ORDER_INFORMATION);
                    showSelectedOption(WORK_ORDER_INFORMATION);
                }
                break;
            case R.id.ll_daily_feed_back:
                if (currentIndex != DAILY_FEED_BACK) {

                    currentIndex = DAILY_FEED_BACK;
                    viewpager.setCurrentItem(DAILY_FEED_BACK);
                    showSelectedOption(DAILY_FEED_BACK);
                }
                break;
            case R.id.ll_completion_order:
                if (currentIndex != COMPLETION_ORDER) {
                    currentIndex = COMPLETION_ORDER;
                    viewpager.setCurrentItem(COMPLETION_ORDER);
                    showSelectedOption(COMPLETION_ORDER);
                }
                break;

        }


    }


    @Override
    public String getOrderId() {
        if (null != orderParentBean)
            return orderParentBean.getId();
        return null;
    }

    @Override
    public void getEngineeringInstallOrderDetailSuccessed(EngineeringInstallationOrderBean engineeringInstallationOrderBean) {
        this.engineeringInstallationOrderBean = engineeringInstallationOrderBean;
        if (workOrderInfomationForEngineeringInstallFragment != null)
            workOrderInfomationForEngineeringInstallFragment.initData(engineeringInstallationOrderBean);

        if (dailyFeedBackForEngineeringInstallFragment != null)
            dailyFeedBackForEngineeringInstallFragment.initData(engineeringInstallationOrderBean);

        if (completionOrderForEngineeringInstallFragment != null)
            completionOrderForEngineeringInstallFragment.initData(engineeringInstallationOrderBean);
//
//        if (backEngineeringOrderFragment != null)
//            backEngineeringOrderFragment.initData(engineeringOrderBeanList_status_returns);
    }

    @Override
    public void getEngineeringInstallOrderDetailFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);//解注册
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, null);
    }
}
